var toggleFBTimeline, askFBTimelinePermission, disableFBTimeline;

$(document).ready(function () {
	$(function () {
		if ($.query.get('msg')!='') {
			$('#account_log').addClass('ajax_error');
			$(window).scrollTop($('#account_log').position().top);
		}
	});

	$('#select_account').click(function () {
		if (!$(this).hasClass('info_tabs_selected')) {
			$('#select_account').addClass('info_tabs_selected').children().addClass('nohover');
			$('#select_notifications').removeClass('info_tabs_selected').children().removeClass('nohover');
			$('#account').show();
			$('#notifications').hide();
			$('#account_log').removeClass().html('');
			$('#notifications_log').removeClass().html('');
		}
	});

	$('#select_notifications').click(function () {
		if (!$(this).hasClass('info_tabs_selected')) {
			$('#select_account').removeClass('info_tabs_selected').children().removeClass('nohover');
			$('#select_notifications').addClass('info_tabs_selected').children().addClass('nohover');
			$('#account').hide();
			$('#notifications').show();
			$('#account_log').removeClass().html('');
			$('#notifications_log').removeClass().html('');
		}
	});

	$('#changeUsername').click(function () {
		$.facebox({ ajax: 'dialog/editusername?id='+userId}); // userId declared in associated php file
		return false;
	});

	$('#addPassword, #changePassword').click(function () {
		$.facebox({ ajax: 'dialog/editpassword?id='+userId}); // userId declared in associated php file
		return false;
	});

	$('#changeEmail').click(function () {
		$.facebox({ ajax: 'dialog/editemail?id='+userId}); 		// userId declared in associated php file
		return false;
	});

	$('#account_save').click(function () {
		var formData = $('#account_form').serialize();
		var pageRefresh = shouldRefreshPage();

		$.ajax({
			type: 'POST',
			url: API2_PATH+'updateuserinfo',
			data: formData,
			success: function () {
			 $('#account_log').removeClass().addClass('ajax_success').html(successMessage);

			 // The page should only be refresh if Tumblr credentials are entered and AJAX response is sucessful.
			 // Otherwise, the Tumblr credentials are reset.
			 if (pageRefresh==true) {
				 window.location = window.location.pathname;
			 }
			 else {
				 $('input[name=tumblremail]').val('');
				 $('input[name=tumblrpassword]').val('');
			 }
			},
			error: function (data) {
			 $('#account_log').removeClass().addClass('ajax_error').html(JSON.parse(data.responseText)['message']);
			}
		});
	});

	$('#add_tumblr_info').click(function () {
		$('#add_tumblr_info').hide();
		$('#tumblr_info').slideDown();
	});

	$('#notifications_form input:checkbox').click(function () {
		if ($(this).is(':checked')) {
			$(this).attr('checked', 'checked').val('1');
		}
		else {
			$(this).removeAttr('checked').val('0');
		}
	});

	$('#notifications_save').click(function () {
		var formData = $('#notifications_form').serialize();

		$.ajax({
			type: 'POST',
			url: API2_PATH+'usernotifications',
			data: formData,
			success: function () {
			 $('#notifications_log').removeClass().addClass('ajax_success').html(successMessage);
			},
			error: function (data) {
			 $('#notifications_log').removeClass().addClass('ajax_error').html(JSON.parse(data.responseText)['message']);
			}
		});
	});

	// initialize the datepicker
	$('#birthdate').datepicker({
		changeMonth: true,
		changeYear: true,
		dateFormat: 'mm-dd-yy',
		maxDate: new Date(),
		showAnim: 'fadeIn',
		yearRange: '1900:+00'
	});

	$('.help').tipsy({ gravity: 'w' });

	toggleFBTimeline = function () {
		if ($('#checkbox_fb_publish_actions').is(':checked')) {
			askFBTimelinePermission();
		}
		else {
			disableFBTimeline();
		}
	}

	askFBTimelinePermission = function () {
		top.location.href = 'https://www.facebook.com/dialog/oauth/?scope=publish_stream&client_id='+FACEBOOK_APP_ID+'&redirect_uri='+timelineCallbackUrl;
	}

	disableFBTimeline = function () {
		$.post('/apiv2/facebook', {req_type:'disable_fb_timeline', 'user_id':userFbId}, function (result) {
			if (result['data']===true) {
				$('#div_remove_fb_timeline_error').hide();
			}
			else {
				$('#div_remove_fb_timeline_error').show();
				$('#checkbox_fb_publish_actions').prop("checked", true);
			}
		});
	}
});

function shouldRefreshPage() {
	var tumblrEmail = $('input[name=tumblremail]').val();
	var tumblrPassword = $('input[name=tumblrpassword]').val();

	if (typeof tumblrEmail=='undefined' || typeof tumblrPassword=='undefined') {
		return false;
	}
	else if (tumblrEmail.length==0 || tumblrPassword.length==0) {
		return false;
	}
	else {
		return true;
	}
}


